using System.Collections.Generic;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using UnityEngine;

namespace Game.Component.Operation
{
	public class Game2Component : OperationComponent
	{
		private const int WOUND_1_NUM = 5;

		private const int WOUND_2_NUM = 4;

		private const int WOUND_3_NUM = 3;

		private Transform wound1;

		private List<Transform> wound1Sutures = new List<Transform>();

		private Transform wound2;

		private List<Transform> wound2Sutures = new List<Transform>();

		private Transform wound3;

		private List<Transform> wound3Sutures = new List<Transform>();

		private Transform sutureDemo;

		private Transform btnBack;

		private List<Transform> efcs = new List<Transform>();

		private void Awake()
		{
			btnBack = base.transform.Find("BtnBack");
			wound1 = base.transform.Find("Wound1");
			wound2 = base.transform.Find("Wound2");
			wound3 = base.transform.Find("Wound3");
			sutureDemo = base.transform.Find("SutureDemo");
			List<int> list = new List<int>();
			list.Add(0);
			list.Add(1);
			list.Add(2);
			list.Add(3);
			List<int> list2 = list;
			list2.RemoveAt(Random.Range(0, list2.Count));
			list = new List<int>();
			list.Add(1);
			list.Add(2);
			list.Add(3);
			List<int> list3 = list;
			for (int i = 0; i < list2.Count; i++)
			{
				int index = Random.Range(0, list3.Count);
				Transform wound = GetWound(list3[index]);
				list3.RemoveAt(index);
				if (wound != null)
				{
					wound.SetLocalXY(GetWoundPosition(list2[i]));
					wound.SetLocalRotationZ(Random.Range(-45f, 45f));
				}
			}
		}

		private Transform GetWound(int index)
		{
			switch (index)
			{
			case 2:
				return wound2;
			case 3:
				return wound3;
			default:
				return wound1;
			}
		}

		private Vector2 GetWoundPosition(int index)
		{
			switch (index)
			{
			case 0:
				return new Vector2(Random.Range(-0.9f, -0.7f), Random.Range(1.8f, 2.3f));
			case 1:
				return new Vector2(Random.Range(0.7f, 0.9f), Random.Range(1.8f, 2.3f));
			case 2:
				return new Vector2(Random.Range(-0.9f, -0.7f), Random.Range(-1.2f, -0.8f));
			case 3:
				return new Vector2(Random.Range(0.7f, 0.9f), Random.Range(-1.2f, -0.8f));
			default:
				return Vector2.zero;
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			if (displayer == null)
			{
				return;
			}
			base.TouchDown(touchEventData);
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 1);
			if (raycastHit2D.collider != null)
			{
				if (raycastHit2D.collider.transform == btnBack)
				{
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
					displayer.ChangeType(OperationDisplayerItem.Type.Main);
				}
				else if (raycastHit2D.collider.transform == wound1)
				{
					CheckWound(wound1, wound1Sutures, 5, touchEventData.curPos);
				}
				else if (raycastHit2D.collider.transform == wound2)
				{
					CheckWound(wound2, wound2Sutures, 4, touchEventData.curPos);
				}
				else if (raycastHit2D.collider.transform == wound3)
				{
					CheckWound(wound3, wound3Sutures, 3, touchEventData.curPos);
				}
			}
		}

		private void CheckWound(Transform wound, List<Transform> sutures, int woundNum, Vector2 position)
		{
			if (sutures.Count < woundNum)
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_suture");
				sutures.Add(CreateSuture(position, wound));
				if (sutures.Count >= woundNum)
				{
					wound.GetComponent<SpriteRenderer>().enabled = false;
					wound.Find("Complete").gameObject.SetActive(true);
					Singleton<AudioManager>.Instance.PlaySound("sound_blink");
					Transform transform = Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", wound.transform.position + new Vector3(0f, 0f, -1E-05f), 3f);
					transform.SetParent(base.transform);
					efcs.Add(transform);
				}
			}
		}

		private Transform CreateSuture(Vector3 position, Transform parent)
		{
			Transform transform = Object.Instantiate(sutureDemo.gameObject).transform;
			transform.position = position;
			transform.SetParent(parent);
			transform.SetLocalRotationZ(Random.Range(80, 100));
			transform.gameObject.SetActive(true);
			return transform;
		}

		private void OnDestroy()
		{
			for (int i = 0; i < efcs.Count; i++)
			{
				if (efcs[i] != null)
				{
					Singleton<GamePoolManager>.Instance.Despawn(efcs[i]);
				}
			}
			efcs.Clear();
		}
	}
}
